package com.ruoyi.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Book;
import com.ruoyi.system.domain.LendRecord;
import com.ruoyi.system.domain.vo.BookVo;
import com.ruoyi.system.service.IBookService;
import com.ruoyi.system.service.ILendRecordService;
import com.ruoyi.system.service.IUserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 书籍Controller
 *
 * @author ruoyi
 * @date 2022-12-08
 */
@RestController
@RequestMapping("/system/book")
public class BookController extends BaseController {
    @Autowired
    private IBookService bookService;

    @Autowired
    private IUserCollectionService userCollectionService;
    @Autowired
    private ILendRecordService lendRecordService;

    /**
     * 查询书籍列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(Book book) {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

    /**
     * 导出书籍列表
     */
    @PreAuthorize("@ss.hasPermi('system:book:export')")
    @Log(title = "书籍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Book book) {
        List<Book> list = bookService.selectBookList(book);
        ExcelUtil<Book> util = new ExcelUtil<Book>(Book.class);
        util.exportExcel(response, list, "书籍数据");
    }

    /**
     * 获取书籍详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        Book book = bookService.selectBookById(id);
        book.setViewNumber(book.getViewNumber() + 1);
        bookService.updateBook(book);
        BookVo bookVo = new BookVo();
        BeanUtil.copyProperties(book,bookVo);
        bookVo.setIsCollect(
                userCollectionService.selectUserCollectionBySnoAndBookId(SecurityUtils.getUsername(), id) ? "1" : "0"
        );
        bookVo.setIsLend(
                CollUtil.isNotEmpty(lendRecordService.selectLendRecordList(new LendRecord()
                        .setBookId(id)
                        .setSno(SecurityUtils.getUsername()))) ? "1" : "0"
        );
        return success(bookVo);
    }

    /**
     * 新增书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:add')")
    @Log(title = "新增书籍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book book) {
        book.setIsbn(IdUtil.getSnowflakeNextIdStr());
        return toAjax(bookService.insertBook(book));
    }

    /**
     * 修改书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:edit')")
    @Log(title = "修改书籍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Book book) {
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除书籍
     */
    @PreAuthorize("@ss.hasPermi('system:book:remove')")
    @Log(title = "批量删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bookService.deleteBookByIds(ids));
    }
}
